﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RM2012Mobile
{
    public partial class frmMain : Form
    {
        #region Fields
        public RM2012Service.RM2012Service objService = new RM2012Service.RM2012Service();
        frmLogin m_FrmLogin = null;
        #endregion

        #region Constructor
        public frmMain()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                DangNhap();
            }
            catch
            {
                if (MessageBox.Show("Kết nối tới máy chủ thất bại. Vui lòng kiểm tra lại kết nối trước khi đăng nhập hệ thống !") == DialogResult.OK)
            	{
                    DangNhap();
            	}
            }
        }
        #endregion

        #region Dang Nhap
        public void DangNhap()
        {
            Cont:
            if (m_FrmLogin == null || m_FrmLogin.IsDisposed)
            {
                m_FrmLogin = new frmLogin();
                m_FrmLogin.lblError.Text = "";
            }

            if (m_FrmLogin.ShowDialog() == DialogResult.OK)
            {
                if (m_FrmLogin.txtUsername.Text == "")
                {
                    m_FrmLogin.lblError.Text = "Chưa nhập tài khoản.";
                    goto Cont;
                }
                else if (m_FrmLogin.txtPassword.Text == "")
                {
                    m_FrmLogin.lblError.Text = "Chưa nhập mật khẩu.";
                    goto Cont;
                }
                else
                {
                    bool check = objService.ExistUserByUserNameAndPassword(m_FrmLogin.txtUsername.Text, Utilities.getMD5Hash(m_FrmLogin.txtPassword.Text));
                    if (check == true)
                    {
                        RM2012Service.UsersInfo userInfo = objService.GetUserByUserName(m_FrmLogin.txtUsername.Text);
                        RM2012Service.UserGroupInfo gInfo = objService.GetGroupByUserId(userInfo.UserId);
                        lblUser.Text = userInfo.FullName;
                        lblGName.Text = gInfo.UserGroupName;
                        Utilities.UsernamePublic = userInfo.UserName;
                        Utilities.FullNamePublic = userInfo.FullName;
                        Utilities.GroupNamePublic = gInfo.UserGroupName;
                        if (gInfo.UserGroupId.Equals("G001"))
                        {
                            lblGName.ForeColor = System.Drawing.Color.Red;
                        }
                        else if (gInfo.UserGroupId.Equals("G002"))
                        {
                            lblGName.ForeColor = System.Drawing.Color.DarkGreen;
                        }
                        else if (gInfo.UserGroupId.Equals("G003"))
                        {
                            lblGName.ForeColor = System.Drawing.Color.Blue;
                        }
                        else
                        {
                            lblGName.ForeColor = System.Drawing.Color.Black;
                        }
                        m_FrmLogin.Visible = false;
                    } 
                    else
                    {
                        m_FrmLogin.lblError.Text = "Thông tin đăng nhập không đúng.";
                        goto Cont;
                    }
                }
            }
            else
            {
                return;
            }
        }
        #endregion

        #region Show From
        private void btnServe_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormServe();
        }

        private void btnTable_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormTables();
        }

        private void btnMenu_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormMenu();
        }

        private void btnBooking_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormBooking();
        }

        private void btnTableEmpty_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormTablesEmpty();
        }

        private void btnTableServing_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormTablesServing();
        }

        private void btnCustomer_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormCustomer();
        }
        #endregion

        #region Exit
        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn thoát ?", "THOÁT", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }
        #endregion

        #region Menu Item Event
        private void menuItemTable_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormTables();
        }

        private void menuItemTableEmpty_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormTablesEmpty();
        }

        private void menuItemTableServing_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormTablesServing();
        }

        private void menuItemMenu_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormMenu();
        }

        private void menuItemCustomer_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormCustomer();
        }

        private void menuItemBooking_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormBooking();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn thoát ?", "THOÁT", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }
        #endregion

        #region Permission
        #endregion
    }
}